package com.wisdytech.linkdcs.equipLifeCycle.maintain.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dto.MaintenancePlanInteriDTO;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dto.MaintenanceTaskDTO;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenanceTask;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.service.IMaintenanceTaskService;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.vo.MaintenanceTaskVO;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.vo.TPReplaceTempVO;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.TPReplaceTemp;
import com.wisdytech.linkdcs.system.model.SysUser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.ITPReplaceTempService;
import com.wisdytech.linkdcs.partLife.service.IPartService;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-06-13
 */
@Controller
@RequestMapping("/maintain/maintenanceTask")
public class MaintenanceTaskController {

	
    /**
     * 保养任务service
     */
    @Autowired
    @Qualifier("iMaintenanceTaskService")
    private IMaintenanceTaskService iMaintenanceTaskService;
    @Autowired
    @Qualifier("iTPReplaceTempService")
    private ITPReplaceTempService iTPReplaceTempService;
    @Autowired
    @Qualifier("iPartService")
    private IPartService iPartService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;

    /**
     * 
     * <p>Description: 设备任务</p>
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String maintainType(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "lifeCycle/equipLifeCycle/maintain/task";
    }
    
    
    /**
     * 分页条件查询保养任务
     * @param taskDTO 备件名称
     * @param filter 分页参数
     * @return 保养任务列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listTaskByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<MaintenanceTaskVO> listTaskByFilter(MaintenanceTaskDTO taskDTO,Filter filter) throws IOException{
        SysUser userSession = BaseUtil.getCurrentUser();
        String loginName = userSession.getTruename();
        //查询周期计划
    	PageInfo<MaintenanceTask> tasks =this.iMaintenanceTaskService.listTaskByFilter(taskDTO,filter,loginName);
        //转换为前台结果集
        List<MaintenanceTask> list=tasks.getList();
        List<MaintenanceTaskVO> VOList=new ArrayList<MaintenanceTaskVO>();
        for(MaintenanceTask maintenanceTask:list){
        	VOList.add(DataTransformUtil.changeTaskVO(maintenanceTask));
        }
        return new StandardTableResult<MaintenanceTaskVO>(tasks.getTotal(),VOList);
    }
    
    /**
     * <p>Description: 保存保养临时任务</p> 
     * @param maintenancePlanInteriDTO 临时保养任务
     * @return 处理结果对象
     */
    @RequestMapping(value = "/savePlanInteri/save.mvc")
    @ResponseBody
    public StandardResult savePlanInteri(MaintenancePlanInteriDTO maintenancePlanInteriDTO) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
       iMaintenanceTaskService.addPlanInteri(maintenancePlanInteriDTO, loginName);
        return StandardResult.getSuccessResult();
    }
    
    
    /**
     * <p>Description: 保存临时备件/工装更换信息</p> 
     * @param tPReplaceTemp 临时保存更换备件信息
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveReplaceTemp/save.mvc")
    @ResponseBody
    public StandardResult saveReplaceTemp(TPReplaceTemp tPReplaceTemp) {
    	//标记为未确认提交状态
    	tPReplaceTemp.setFlag("0");
    	if(StringUtils.isBlank(tPReplaceTemp.getPkId())) {
    		tPReplaceTemp.setPkId(BaseUtil.getUUID());
    		if(tPReplaceTemp.getType().equals("0")) {
    			
    			iTPReplaceTempService.add(tPReplaceTemp);
    		}else {
    			iTPReplaceTempService.addTool(tPReplaceTemp);
    		}
    	}else {
    		iTPReplaceTempService.update(tPReplaceTemp);
    	}
    	
        return StandardResult.getSuccessResult();
    }
    
    /**
     * 分页条件查询临时保存的工装及备件
     * @param taskDTO 备件名称
     * @param filter 分页参数
     * @return 临时保存的工装及备件
     * @throws IOException 
     */
    @RequestMapping(value = "/getTemps/get.mvc")
    @ResponseBody	
    public StandardTableResult<TPReplaceTempVO> getTemps(Filter filter,String planId) throws IOException{
    	PageInfo<TPReplaceTemp> temps =this.iTPReplaceTempService.getTemps(filter, planId);
        //转换为前台结果集
        List<TPReplaceTemp> list=temps.getList();
        List<TPReplaceTempVO> VOList=new ArrayList<TPReplaceTempVO>();
        for(TPReplaceTemp tPReplaceTemp:list){
        	VOList.add(DataTransformUtil.changeReplaceTempVO(tPReplaceTemp));
        }
        return new StandardTableResult<TPReplaceTempVO>(temps.getTotal(),VOList);
    }
    
    /**
     * 分页条件查询临时保存的工装及备件
     * @param taskDTO 备件名称
     * @param filter 分页参数
     * @return 临时保存的工装及备件信息
     * @throws IOException 
     */
    @RequestMapping(value = "/getTempsByPkId/get.mvc")
    @ResponseBody	
    public StandardResult getTempsByPkId(String pkId) throws IOException{
    	TPReplaceTemp temp =this.iTPReplaceTempService.getTempAllById(pkId);
        return StandardResult.getSuccessResult(temp);
    }
    
    /**
     * <p>Description: 删除备件或工装</p> 
     * @param pkId 临时保存更换备件信息
     * @return 处理结果对象
     */
    @RequestMapping(value = "/delReplaceTemp/del.mvc")
    @ResponseBody
    public StandardResult delReplaceTemp(String pkId) {
        List<String> idList = new ArrayList<String>();
        String[] ids = pkId.split(",");
        for (int i = 0; i < ids.length; i++) {
            idList.add(ids[i]);
        }
        for(String id : ids) {
        	iTPReplaceTempService.del(id);
        }
        return StandardResult.getSuccessResult();
    }
    
}

